package com.leonzx.base.controller.base;

import com.google.gson.Gson;
import com.leonzx.base.controller.api.APPConstants;
import com.leonzx.base.controller.api.Result;
import com.leonzx.base.entity.system.User;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制器基类
 * <p>
 * Created by liang on 2016/9/25.
 */
public class BaseController {

    protected final Logger logger = getLogger();
    protected final Gson gson = new Gson();

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        datetimeFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(datetimeFormat, true));
    }

    /**
     * 获取当前登陆用户
     * <p>
     * <p>
     */
    protected User currentUser() {
        return (User) SecurityUtils.getSubject().getPrincipal();
    }

    protected void addSuccessMessage(RedirectAttributes redirectAttributes, String msg) {
        redirectAttributes.addFlashAttribute("message", msg);
        redirectAttributes.addFlashAttribute("code", "1");
    }

    protected void addFailMessage(RedirectAttributes redirectAttributes, String msg) {
        redirectAttributes.addFlashAttribute("message", msg);
        redirectAttributes.addFlashAttribute("code", "0");
    }

    protected void addSuccessMessage(Model model, String msg) {
        model.addAttribute("message", msg);
        model.addAttribute("code", "1");
    }

    protected void addFailMessage(Model model, String msg) {
        model.addAttribute("message", msg);
        model.addAttribute("code", "0");
    }

    public void addSuccessMessage(Map<String, Object> result, String msg) {
        result.put("msg", msg);
        result.put("code", 1);
    }

    public void addFailMessage(Map<String, Object> result, String msg) {
        result.put("msg", msg);
        result.put("code", 0);
    }

    public Logger getLogger() {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        return logger;
    }

    /**
     * 封装成功时返回的对象（返回json数据时使用）
     */
    protected Result success(String msg, Object data) {
        return new Result(APPConstants.DEFAULT_SUCCESS_CODE, msg, data);
    }

    protected Result success(String msg) {
        return new Result(APPConstants.DEFAULT_SUCCESS_CODE, msg);
    }

    /**
     * 封装失败时返回的对象（返回json数据时使用）
     */
    protected Result error(String msg, Object data) {
        return new Result(APPConstants.DEFAULT_ERROR_CODE, msg, data);
    }

    protected Result error(String msg) {
        return new Result(APPConstants.DEFAULT_ERROR_CODE, msg);
    }

    protected Map<String, Object> getMap() {
        return new HashMap<>();
    }
}
